home *** CD-ROM | disk | FTP | other *** search
- Path: news.dseg.ti.com!news
- From: grubin@ti.com (Geoffrey Rubin)
- Newsgroups: comp.lang.c++
- Subject: Re: [Q] OOD/inheritance Problem
- Date: 22 Feb 1996 13:56:14 GMT
- Organization: AWP
- Message-ID: <4ghslu$dld@mksrv1.dseg.ti.com>
- References: <4g53i1$jrr@ixnews3.ix.netcom.com>
- NNTP-Posting-Host: cna0185662.dseg.ti.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <4g53i1$jrr@ixnews3.ix.netcom.com>, fedinv@ix.netcom.co
- says...
- >
- >I have two classes which form a handle/body pair. So, one class (
- class
- >A, the handle) is seen by clients, and class A HAS-A class B ( the
- body
- >) which is hidden from clients. Hence we have the following situation
- -
- >
- >// ...Handle
- >class A
- >{
- > A( int n ) { if ( n == x ) b = new ( C ) ;
- > elseif ( n == y ) b = new ( D ) ;
- > else b = new (E) ; } ;
- > ~A() { } ;
- > .
- > .
- > A_f1 ( ) { b->B_f1( ) ; } ; // forward request to B
- > A_f2 ( ) { b->B_f2( ) ; } ; // ô ô
- > .
- > .
- > private:
- > B *b ; // A HAS-A B
- >} ;
- >
- >//... Body ( is an ABC )
- >class B
- >{
- > .
- > .
- > virtual B_f1 ( ) = 0 ;
- > virtual B_f2 ( ) ;
- > .
- > .
- >} ;
- >
- >Class AÆs purpose is to shield clients from the details of class B
- and
- >all it does is merely forward requests to B. Class B is an abstract
- >base class for class C, D and E. Classes C,D and E implement the
- >functions B_f1, B
- >_f2 etc.. However, class E has its own member functions. Hence class
- E
- >would look something like this -
- >
- >class E : public B
- >{
- > .
- > .
- > B_f1 ( ) { } ;
- > B_f2 ( ) { } ;
- > E_f1 ( ) { } ;
- > .
- > .
- >} ;
- >
- >I would like to invoke E-f1() using the b pointer in class A. Hence
- >inside A, I would like to do the following -
- >class A
- >{
- > .
- > .
- > A_f3 ( ) { b->E_f1 () ; } ;
- >} ;
- >
- >This WILL NOT work ... snip
-
- There is two direct ways to do this. The first is the object oriented
- way.
-
- You must add in a E_f1 method in the base class. The base class can
- define this operation as a null behavior. This allows you to perform
- this operation on any object inherited from B.
-
- The second way (assuming that the first way is not applicable), is to
- define a method in B that returns a type (code). This allows you to
- query the object and if it is of type E, to perform the type cast and
- use the method E_f1. This type of code should be avoided since it is
- harder to maintain.
-
- I have used this second method in code. I didn't like it at all.
- Therefore I redesigned the objects so that the first method could be
- used. I will give a brief example.
-
- Second Method: (ugly)
- A Shape base class performs creation, shape kind determination
- and drawing of itself. Many different shapes are derived (inheritied)
- from this Shape class. Interactive Editing routines perform
- translation, scaling and rotation operations that are shape specific.
- Therefore the editing functions used a switch statement based on shape
- type to perform these operations (ugly). This is bad since adding
- a new shape, requires changing the editing operations.
-
- First (prefered method):
- Redesign the shape base class to include general methods for
- translation, scaling and rotation. The editing routines then simply
- determine what to do. For example; traslate over 5.5 and down 10.2
- units or rotated by 13.3 radians.
-
- The specific shapes derived from the Shape class must override the
- default behavior whenever this is not the desired result.
-
- For example the Shape translation method may work for all shapes
- (assumming the position is within the Shape base class. The Shape
- rotation method may provide no action since many objects are rotation
- independent (a point, a circle etc.).
-
- Geoffrey
-
-
-